package com.alinu.workbench.web.controller;

import com.alinu.settings.domain.User;
import com.alinu.utils.DateTimeUtil;
import com.alinu.utils.PrintJson;
import com.alinu.utils.ServiceFactory;
import com.alinu.utils.UUIDUtil;
import com.alinu.vo.PaginationVO;
import com.alinu.workbench.domain.Activity;
import com.alinu.workbench.domain.ActivityRemark;
import com.alinu.workbench.service.ActivityService;
import com.alinu.workbench.service.impl.ActivityServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenzuwei
 * @date 2021/3/15
 * @description 市场活动控制器类
 */
public class ActivityController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String path = request.getServletPath();
        assert path != null;
        switch (path) {
            case "/workbench/activity/saveActivity.do":
                saveActivity(request, response);
                break;
            case "/workbench/activity/getActivityList.do":
                getActivityList(request, response);
                break;
            case "/workbench/activity/deleteActivity.do":
                deleteActivity(request, response);
                break;
            case "/workbench/activity/findActivity.do":
                findActivity(request, response);
                break;
            case "/workbench/activity/updateActivity.do":
                updateActivity(request, response);
                break;
            case "/workbench/activity/detail.do":
                detail(request, response);
                break;
            case "/workbench/activity/getActivityRemarkList.do":
                getActivityRemarkList(request, response);
                break;
            case "/workbench/activity/deleteRemark.do":
                deleteRemark(request, response);
                break;
            case "/workbench/activity/saveRemark.do":
                saveRemark(request, response);
                break;
            case "/workbench/activity/updateRemark.do":
                updateRemark(request, response);
                break;
            default:
                break;
        }
    }

    private void updateRemark(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String id = request.getParameter("id");
        String noteContent = request.getParameter("noteContent");
        String editTime = DateTimeUtil.getSysTime();
        String editBy = ((User) request.getSession().getAttribute("user")).getName();
        String editFlag = "1";

        ActivityRemark remark = new ActivityRemark();

        remark.setId(id);
        remark.setNoteContent(noteContent);
        remark.setEditBy(editBy);
        remark.setEditTime(editTime);
        remark.setEditFlag(editFlag);

        boolean result = activityService.updateRemark(remark);

        PrintJson.printJsonResult(response, result);
    }

    private void saveRemark(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String activityId = request.getParameter("activityId");
        String noteContent = request.getParameter("noteContent");
        String id = UUIDUtil.getUUID();
        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User) request.getSession().getAttribute("user")).getName();
        String editFlag = "0";

        ActivityRemark remark = new ActivityRemark();
        remark.setActivityId(activityId);
        remark.setCreateBy(createBy);
        remark.setCreateTime(createTime);
        remark.setEditFlag(editFlag);
        remark.setId(id);
        remark.setNoteContent(noteContent);

        boolean result = activityService.saveRemark(remark);

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("result", result);
        resultMap.put("ar", remark);

        PrintJson.printJsonObj(response, resultMap);


    }

    private void deleteRemark(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String id = request.getParameter("id");

        boolean result = activityService.deleteRemark(id);

        PrintJson.printJsonResult(response, result);

    }

    private void getActivityRemarkList(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        String activityId = request.getParameter("activityId");

        List<ActivityRemark> remarkList = activityService.getRemarkList(activityId);

        PrintJson.printJsonObj(response, remarkList);

    }

    private void detail(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String id = request.getParameter("id");

        Activity activity = activityService.findActivity(id);

        request.setAttribute("activity", activity);

        // 请求转发
        request.getRequestDispatcher("/workbench/activity/detail.jsp").forward(request, response);
    }

    private void updateActivity(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        Activity activity = new Activity();

        activity.setId(request.getParameter("id"));
        activity.setOwner(request.getParameter("owner"));
        activity.setName(request.getParameter("name"));
        activity.setStartDate(request.getParameter("startDate"));
        activity.setEndDate(request.getParameter("endDate"));
        activity.setCost(request.getParameter("cost"));
        activity.setDescription(request.getParameter("description"));

        boolean result = activityService.updateActivity(activity);

        PrintJson.printJsonResult(response, result);

    }

    private void findActivity(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        String id = request.getParameter("id");

        Activity activity = activityService.findActivity(id);

        PrintJson.printJsonObj(response, activity);

    }

    private void deleteActivity(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        // 接收多个参数
        String[] ids = request.getParameterValues("id");

        boolean result = activityService.deleteActivity(ids);

        PrintJson.printJsonResult(response, result);

    }

    private void getActivityList(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());

        int pageNo = Integer.parseInt(request.getParameter("pageNo"));
        int pageSize = Integer.parseInt(request.getParameter("pageSize"));

        int start = (pageNo - 1) * pageSize;
        Map<String, Object> params = new HashMap<>();
        params.put("start", start);
        params.put("pageSize", pageSize);
        params.put("name", request.getParameter("name"));
        params.put("owner", request.getParameter("owner"));
        params.put("startDate", request.getParameter("startDate"));
        params.put("endDate", request.getParameter("endDate"));

        PaginationVO<Activity> activityOutput = activityService.getActivityList(params);
        PrintJson.printJsonObj(response, activityOutput);

    }

    private void saveActivity(HttpServletRequest request, HttpServletResponse response) {
        ActivityService activityService = (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        String id = UUIDUtil.getUUID();
        String owner = request.getParameter("owner");
        String name = request.getParameter("name");
        String startDate = request.getParameter("startDate");
        String endDate = request.getParameter("endDate");
        String cost = request.getParameter("cost");
        String description = request.getParameter("description");
        String createTime = DateTimeUtil.getSysTime();
        String createBy = ((User) request.getSession().getAttribute("user")).getName();

        Activity activity = new Activity();
        activity.setCost(cost);
        activity.setCreateBy(createBy);
        activity.setCreateTime(createTime);
        activity.setDescription(description);
        activity.setOwner(owner);
        activity.setId(id);
        activity.setName(name);
        activity.setStartDate(startDate);
        activity.setEndDate(endDate);

        boolean flag = activityService.saveActivity(activity);
        PrintJson.printJsonResult(response, flag);
    }

}
